package com.zhongkun.datahome.service.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongkun.datahome.service.domain.HouseServerRelation;
import com.zhongkun.datahome.service.mapper.HouseServerRelationMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/14 17:15
 * @Desc
 */
@Repository
public class HouseServerRelationRepository extends ServiceImpl<HouseServerRelationMapper, HouseServerRelation> {
    public List<Long> listServerUserId(Long houseId) {
        return this.list(new LambdaQueryWrapper<HouseServerRelation>()
                .eq(HouseServerRelation::getHouseId, houseId))
                .stream()
                .map(HouseServerRelation::getServerUserId)
                .collect(Collectors.toList());
    }

    public List<HouseServerRelation> listBy(Long houseId, Long tenantUserId) {
        return this.list(new LambdaQueryWrapper<HouseServerRelation>()
                        .eq(HouseServerRelation::getHouseId, houseId)
                        .eq(HouseServerRelation::getServerUserId, tenantUserId));
    }

    public void removeByHouseIds(List<Long> houseIds) {
        if (CollectionUtils.isNotEmpty(houseIds)) {
            this.remove(new LambdaQueryWrapper<HouseServerRelation>()
                    .in(HouseServerRelation::getHouseId, houseIds));
        }
    }

    public void removeBy(Long houseId, Long tenantUserId) {
        this.remove(new LambdaQueryWrapper<HouseServerRelation>()
                .eq(HouseServerRelation::getHouseId, houseId)
                .eq(HouseServerRelation::getServerUserId, tenantUserId));
    }
}
